<?php

/**
 * 商品列表
 */

namespace app\shop\common;

class IndexCommon extends \app\base\common\SiteCommon {

    protected $pageLimit = 20;

    protected function index() {
        $classId = request('get', 'id', 0, 'intval');

        $service = target('shop/List', 'service');

        $filterWhere = [];
        if ($classId) {
            $classInfo = $service->getInfo($classId);
            if (empty($classInfo)) {
                $this->error404();
            }
            if ($classInfo['url']) {
                $this->redirect($classInfo['url']);
            }
            $tpl = 'shop_' . $this->siteConfig['tpl_class'];
            $this->setMeta($classInfo['name']);
            $crumb = target('shop/ShopClass')->loadCrumbList($classId);

            $parentClassInfo = array_slice($crumb, -2, 1);
            if (empty($parentClassInfo)) {
                $parentClassInfo = $crumb[0];
            } else {
                $parentClassInfo = $parentClassInfo[0];
            }
            $topClassInfo = $crumb[0];
            $this->pageInfo['name'] = $classInfo['name'];

            $this->setCrumb($crumb);

            if ($classInfo['tpl_class']) {
                $tpl = $classInfo['tpl_class'];
            }
            $modelId = $classInfo['model_id'];

            $classIds = target('shop/ShopClass')->getSubClassId($classId);
            if ($classIds) {
                $filterWhere['_sql'] = 'A.class_id in(' . $classIds . ')';
            }

            $this->assign('classInfo', $classInfo);
            $this->assign('parentClassInfo', $parentClassInfo);
            $this->assign('topClassInfo', $topClassInfo);
        }else {
            $tpl = 'shop_list';
            $modelId = 0;
            $this->pageInfo['name'] = '全部商品';
            $this->setMeta('全部商品');
            $this->setCrumb([
                [
                    'name' => '全部商品',
                    'url' => url()
                ]
            ]);
        }

        $data = $service->getData($filterWhere, ['id' => $classId]);

        $filter = $data['filter'];
        $where = $data['where'];
        $filterStatus = $data['filterStatus'];
        $order = $data['order'];

        $priceList = $service->getPriceData($filter['ids']);
        $brandList = $service->getBrandData($filter['ids']);

        $this->assign('brandList', $brandList);
        $this->assign('filterStatus', $filterStatus);


        $orderList = $service->getOrderData();

        $pageLimit = $this->pageLimit;
        $model = target('shop/Shop');
        if($where) {
            $count = $model->countList($where);
            $pageData = $this->pageData($count, $pageLimit, $filter['urlParam']);
            $list = $model->loadList($where, $pageData['limit'], $order, $modelId);
        }else {
            $pageData = [];
            $list = [];
        }

        $this->assign('attrList', $filter['attrList']);
        $this->assign('priceList', $priceList);
        $this->assign('orderList', $orderList);
        $this->assign('classId', $classId);
        $this->assign('pageParams', $filter['urlParam']);
        $this->assign('page', $pageData['html']);
        $this->assign('pageList', $list);
        $this->siteDisplay($tpl);
    }


}